import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useUserStore } from "@/stores";
import { ElMessage } from "element-plus";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "登录",
    meta: { title: "登录" },
    component: () => import("@/views/login/loginView.vue"),
  },
  {
    path: "/",
    component: () => import("@/views/layout.vue"),
    redirect: "postList",
    children: [
      {
        path: "postList",
        name: "postList",
        meta: { title: "帖子列表", icon: "Message" },
        component: () => import("@/views/postList/postList.vue"),
      },
      {
        path: "auditList",
        name: "auditList",
        meta: { title: "审核列表", icon: "Document" },
        component: () => import("@/views/auditList/auditList.vue"),
      },
      {
        path: "clubList",
        name: "clubList",
        meta: { title: "社团列表", icon: "MessageBox" },
        component: () => import("@/views/clubList/clubList.vue"),
      },
      {
        path: "userList",
        name: "userList",
        meta: { title: "用户列表", icon: "User" },
        component: () => import("@/views/userList/userList.vue"),
      },
    ],
  },
];
const router = createRouter({
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory("/dist"),
  routes,
});
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  if (!userStore.accessToken && to.path != "/login") {
    ElMessage.error("请先登录");
    next("/login");
  }
  document.title = "学生荟&" + to.meta.title;
  next();
});

export default router;
